package com.inossem.warehousestorage_inossem.adapter.changeStorage.chuangyou;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.change_storage.chuangyou.CYChangeStorageMaterielListActivity;
import com.inossem.warehousestorage_inossem.bean.outStorage.ItemListBean;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @author 王斯宇
 * @time on 2018/3/22 10:40
 * @email siyu.wang@inossem.com
 */
public class CYChangeStorageListAdapter extends BaseAdapter {

    private Context mContext;
    private List<ItemListBean> list = new ArrayList<>();
    private LayoutInflater inflater;
    private String mCondition;
    private EditText codeEditText;
    private CYChangeStorageMaterielListActivity.ChangeStorageMaterielListener listener;

    public CYChangeStorageListAdapter(Context context , String condition , List<ItemListBean> list , CYChangeStorageMaterielListActivity.ChangeStorageMaterielListener listener) {
        this.mContext = context;
        this.inflater = LayoutInflater.from(context);
        this.listener = listener;
        this.list = list;
        this.mCondition = condition;

    }

    @Override
    public int getCount() {
        return list.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.cy_view_change_storage_list_first_item, null);
            codeEditText = (EditText) convertView.findViewById(R.id.code);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            codeEditText.setHint("请输入物料编码/物料描述");
            if(!TextUtils.isEmpty(mCondition)){
                codeEditText.setText(mCondition);
            }
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    listener.onNoDoubleClickListener(TextUtils.isEmpty(codeEditText.getText().toString()) ? "" : codeEditText.getText().toString());

                }
            });
        } else {
            ItemListBean materielItem = list.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.cy_view_change_storage_materiel_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, materielItem);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.materialCode = (TextView) view.findViewById(R.id.materialCode);
        viewHolder.materialDescribe = (TextView) view.findViewById(R.id.materialDescribe);
        viewHolder.outStorageNumber = (TextView) view.findViewById(R.id.outStorageNumber);
        viewHolder.materialLayout = (PercentRelativeLayout) view.findViewById(R.id.materialLayout);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, ItemListBean item) {
        viewHolder.materialCode.setText(item.getMat_code());
        viewHolder.materialDescribe.setText(item.getMat_name());
        viewHolder.outStorageNumber.setText(TextUtils.isEmpty(item.getActualSendNumber()) ? "请输入数量" : item.getActualSendNumber() + item.getUnit_name());
        if (!TextUtils.isEmpty(item.getActualSendNumber()) && Float.parseFloat(item.getActualSendNumber()) > 0) {
            viewHolder.materialLayout.setBackgroundColor(context.getResources().getColor(R.color.test_green));
        }else {
            viewHolder.materialLayout.setBackgroundColor(context.getResources().getColor(R.color.base_gray));
        }
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                listener.onViewClickListener(item.getMat_id() , item.getSupplier_code());
            }
        });
    }

    private class ViewHolder {
        private TextView materialCode , materialDescribe, outStorageNumber ;
        private PercentRelativeLayout materialLayout;
    }
}
